package com.bite.common.utils;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JsonUtil {
    /**
     * 对象转json
     */
    public static String toJsonString(Object o){
        if (null == o){
            return null;
        }
        try {
            return JSON.toJSONString(o);
        }catch (Exception e){
            log.error("JsonUtil.toJsonString error: ", e);
            return null;
        }
    }


    /**
     * json转对象
     */
    public static <T> T toObject(String s, Class<T> clazz){
        if (null == s || clazz == null){
            return null;
        }
        try {
            return JSON.parseObject(s, clazz);
        }catch (Exception e){
            log.error("JsonUtil.toObject error: s: {}", s, e);
            return null;
        }
    }
}
